<?php

/**
 * TODO 怀疑都是无用的, 以后删除
 *
 * alltosun.com 商品widget goods.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 吴松键 (wusj@alltosun.com) $
 * $Date: 2014-9-3 上午11:07:58 $
 * $Id$
 */
class goods_widget
{
    private $per_page = 10;
    private $page_no = 1;

    public function get_comment_list($params = array()) {

    }

    /**
     * 常见问题
     * @param unknown $params
     * @return unknown
     */
    public function get_ask_list($params = array()) {
        if (isset($params['goods_id']) && $params['goods_id']) {
            $filter['goods_id'] = $params['goods_id'];
        }
        $filter['status'] = 1;

        $list = array();
        $list = _model('ask')->getList($filter, ' ORDER BY `view_order` ASC ');

        return $list;
    }

    public function get_servers_list($params = array()) {

    }

    public function get_related_goods_list($params = array()) {
        $goods_id = 0;
        $goods_num = 2;
        if (isset($params['goods_id']) && $params['goods_id']) {
            $goods_id = $params['goods_id'];
        }

        if (isset($params['$goods_num']) && $params['$goods_num']) {
            $goods_num = $params['$goods_num'];
        }

        $order_ids = _model('order_relation')->getFields('order_id', array('goods_id' => $goods_id));

        if ($order_ids) {
            $user_ids = _model('order')->getFields('user_id', $order_ids);

            $new_order_ids = _model('order')->getFields('id', array('user_id' => $user_ids));

            $goods_ids = _model('order_relation')->getFields('goods_id', array('order_id' => $new_order_ids));

            $goods_ids = array_count_values($goods_ids);

            arsort($goods_ids);

            $goods_ids = array_flip($goods_ids);
            $tmp = array();
            $list = array();

            foreach ($goods_ids as $k => $v) {
                $goods_info = _uri('goods', array('id' => $v, 'status' => 1));
                if ($v == $goods_id || !$goods_info) {
                    continue;
                }
                $tmp[] = $v;
            }
            for ($i = 0; $i < $goods_num; $i++) {
                $list[] = _uri('goods', $tmp[$i]);
            }
        } else {
            $list = _model('goods')->getList(array('id >' => 0), ' ORDER BY `is_recommend` DESC LIMIT ' . $goods_num);
        }
        return $list;
    }

    /**
     * TODO 无用的
     *
     * 获取最新商品列表
     */
    public function get_new_list($params = array()) {
        $this->page_no = Request::Get('page_no', 1);
        if (isset($params['per_page']) && $params['per_page']) {
            $this->per_page = $params['per_page'];
        }
        if (isset($params['page_no']) && $params['page_no']) {
            $this->page_no = $params['page_no'];
        }
        if (isset($params['category_id']) && $params['category_id']) {
            $filter['category_id'] = $params['category_id'];
        }
        if (isset($params['series_id']) && $params['series_id']) {
            $filter['series_id'] = $params['series_id'];
        }
        if (isset($params['search']) && $params['search']) {
            $filter['(`title` LIKE '] = '%' . $params['search'] . '%';
            $filter[' OR `auxi_title` LIKE '] = '%' . $params['search'] . '%';
            $filter[' ) AND `status`'] = 1;
        } else {
            $filter['status'] = 1;
        }
        if (isset($params['order']) && $params['order']) {
            $sort = $params['order_status'] == 2 ? 'ASC' : 'DESC';
            switch ($params['order']) {
                case 'new':
                case 'time':
                    $order = ' ORDER BY `add_time` ' . $sort . ' ';
                    break;

                case 'price':
                    $order = ' ORDER BY `price` ' . $sort . ' ';
                    break;

                default:
                    $order = ' ORDER BY `recommend` ' . $sort . ',`add_time` DESC ';
                    break;
            }
        }
        if ((isset($params['start_price']) && $params['start_price']) ||
            (isset($params['end_price']) && $params['end_price'])
        ) {
            $filter['id'] = goods_helper::get_goods_by_price($params['start_price'], $params['end_price']);
        }
        $filter['is_sale'] = 1;

        $new_goods_list = array();
        $goods_count = _model('goods')->getTotal($filter);
        if ($goods_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($goods_count)) {
                Response::assign('pager', $pager);
            }

            $new_goods_list = _model('goods')->getList($filter, $order . $pager->getLimit($this->page_no));
        }
        foreach ($new_goods_list as $k => $v) {
            $new_goods_list[$k]['series_title'] = _uri('series', $v['series_id'], 'title');
            $new_goods_list[$k]['cover'] = _uri('file', $v['cover'], 'path');
            if (!$new_goods_list[$k]['cover']) {
                $file_id = _uri('file_relation', array('res_name' => 'goods', 'res_id' => $v['id']), 'file_id');
                $new_goods_list[$k]['cover'] = _uri('file', $file_id, 'path');
            }
        }

        return $new_goods_list;
    }

    /**
     * TODO 无用的
     *
     * 获取最新商品列表
     */
    public function get_star_list($params = array()) {
        if (isset($params['per_page']) && $params['per_page']) {
            $this->per_page = $params['per_page'];
        }
        if (isset($params['page_no']) && $params['page_no']) {
            $this->page_no = $params['page_no'];
        }
        if (isset($params['category_id']) && $params['category_id']) {
            $filter['category_id'] = $params['category_id'];
        }
        if (isset($params['search']) && $params['search']) {
            $filter['(`title` LIKE '] = '%' . $params['search'] . '%';
            $filter[' OR `auxi_title` LIKE '] = '%' . $params['search'] . '%';
            $filter[' ) AND `status`'] = 1;
        } else {
            $filter['status'] = 1;
        }
        if ((isset($params['start_price']) && $params['start_price']) ||
            (isset($params['end_price']) && $params['end_price'])
        ) {
            $filter['id'] = goods_helper::get_goods_by_price($params['start_price'], $params['end_price']);
        }
        $filter['is_sale'] = 1;

        $star_list = array();
        $goods_count = _model('goods')->getTotal($filter);
        if ($goods_count) {
            $pager = new Pager($this->per_page);
            $order = ' ORDER BY `sale_count` DESC, `add_time` DESC ';
            $star_list = _model('goods')->getList($filter, $order . $pager->getLimit($this->page_no));
        }

        return $star_list;
    }

    /**
     * 获取推荐产品列表
     */
    public function get_recommend_list($params = array()) {
        /* if (isset($params['per_page']) && $params['per_page']) {
            $limit = ' LIMIT 0,'.$params['per_page'];
        }*/
        if (isset($params['category_id']) && $params['category_id']) {
            $filter['category_id'] = $params['category_id'];
        }
        if (isset($params['series_id']) && $params['series_id']) {
            $filter['series_id'] = $params['series_id'];
        }
        if (isset($params['cover_position']) && $params['cover_position']) {
            $cover_position = $params['cover_position'];
            switch ($cover_position % 4) {
                case '1':
                    $cover_position = 4;
                    $cover_size = 1;
                    break;

                case '2':
                    $cover_position = 1;
                    $cover_size = 2;
                    break;

                case '3':
                    $cover_position = 1;
                    $cover_size = 2;
                    $cover_class = 1;
                    break;

                case '0':
                    $cover_position = 6;
                    $cover_size = 1;
                    break;
            }
        }
        $filter['recommend'] = 1;
        $filter['status'] = 1;
        $filter['is_sale'] = 1;
        $filter['id'] = goods_helper::get_goods_ids_by_stock();

        if ($cover_size == 1) {
            $limit = ' LIMIT 0,8';
        } elseif ($cover_size == 2) {
            $limit = ' LIMIT 0,6';
        }

        $recommend_list = array();
        $goods_list = _model('goods')->getList($filter, ' ORDER BY `add_time` DESC ' . $limit);
        if (!empty($goods_list)) {
            foreach ($goods_list as $k => $v) {
                $recommend_cover = _model('file_relation')->getList(array(
                    'res_id' => $v['id'],
                    'res_name' => 'recommend'
                ), ' ORDER BY `add_time` DESC LIMIT 1');
                if ($recommend_cover) {
                    $goods_list[$k]['recommend_cover'] = _uri('file', $recommend_cover[0]['file_id'], 'path');
                } else {
                    $goods_list[$k]['recommend_cover'] = _uri('file', $v['cover'], 'path');
                }
            }

            $recommend_list = $goods_list;
        }

        // 如果没有设置推荐 则自动按照添加时间推荐商品
        $is_empty = 1;
        if ($cover_size == 1 && count($recommend_list) < 8) {
            $num = 8 - count($recommend_list);
            $limit = ' LIMIT 0,' . $num;
            $is_empty = 0;
        } elseif ($cover_size == 2 && count($recommend_list) < 6) {
            $num = 6 - count($recommend_list);
            $limit = ' LIMIT 0,' . $num;
            $is_empty = 0;
        }
        if (!$is_empty) {
            $filter['recommend'] = 0;
            $attach_goods_list = _model('goods')->getList($filter, ' ORDER BY `add_time` DESC ' . $limit);
            $new_recommend_list = array();
            if (!empty($attach_goods_list)) {
                foreach ($recommend_list as $k => $v) {
                    $new_recommend_list[] = $v;
                }
                foreach ($attach_goods_list as $k => $v) {
                    $v['recommend_cover'] = _uri('file', $v['cover'], 'path');
                    $new_recommend_list[] = $v;
                }
                $recommend_list = $new_recommend_list;
            }
        }

        // 添加封面位置
        $i = 0;
        foreach ($recommend_list as $k => $v) {
            if ($cover_position - 1 == $k) {
                $new_list[$i] = array(
                    'is_cover' => 1,
                    'size' => $cover_size,
                    'class' => $cover_class,
                    'series_id' => $params['series_id']
                );
                $i++;
            }
            $new_list[$i] = $v;
            $i++;
        }

        return $new_list;
    }

    /**
     * 获取同类商品列表
     */
    public function get_similar_list($params = array()) {
        if (isset($params['goods_id']) && $params['goods_id']) {
            $goods_id = $params['goods_id'];
        }
        if (isset($params['goods_num']) && $params['goods_num']) {
            $goods_num = $params['goods_num'];
        }

        $goods_info = _uri('goods', $goods_id);
        $filter = array(
            'category_id' => $goods_info['category_id'],
            'status' => 1,
            'is_sale' => 1,
            '`id` <>' => $goods_id,
        );

        $total = _model('goods')->getTotal($filter);
        if ($total > $goods_num) {
            $new_goods_list = _model('goods')->getList($filter, ' ORDER BY `add_time` DESC LIMIT 0,' . $goods_num);
        } else {
            $goods_list = _model('goods')->getList($filter, ' ORDER BY `add_time` DESC LIMIT 0,' . $goods_num);
            unset($filter['category_id']);
            $filter['`category_id` <>'] = $goods_info['category_id'];
            $filter['series_id'] = $goods_info['series_id'];
            $add_goods_list = _model('goods')->getList($filter, ' ORDER BY `add_time` DESC LIMIT 0,' .
                                                                ($goods_num - $total));
            $new_goods_list = array();
            foreach ($goods_list as $k => $v) {
                $new_goods_list[] = $v;
            }
            foreach ($add_goods_list as $k => $v) {
                $new_goods_list[] = $v;
            }
        }
        foreach ($new_goods_list as $k => $v) {
            $new_goods_list[$k]['cover'] = _uri('file', $v['cover'], 'path');
        }

        return $new_goods_list;
    }

    /**
     * 获取搭配商品列表
     */
    public function get_matched_list($params = array()) {
        if (isset($params['goods_id']) && $params['goods_id']) {
            $goods_id = $params['goods_id'];
        }
        if (isset($params['goods_num']) && $params['goods_num']) {
            $goods_num = $params['goods_num'];
        }

        $goods_info = _uri('goods', $goods_id);
        $filter = array(
            'series_id' => $goods_info['series_id'],
            'status' => 1,
            'is_sale' => 1,
            '`id` <>' => $goods_id,
        );

        $total = _model('goods')->getTotal($filter);
        if ($total > $goods_num) {
            $new_goods_list = _model('goods')->getList($filter, ' ORDER BY `add_time` DESC LIMIT 0,' . $goods_num);
        } else {
            $goods_list = _model('goods')->getList($filter, ' ORDER BY `add_time` DESC LIMIT 0,' . $goods_num);
            unset($filter['series_id']);
            $filter['`series_id` <>'] = $goods_info['series_id'];
            $filter['category_id'] = $goods_info['category_id'];
            $add_goods_list = _model('goods')->getList($filter, ' ORDER BY `add_time` DESC LIMIT 0,' .
                                                                ($goods_num - $total));
            $new_goods_list = array();
            foreach ($goods_list as $k => $v) {
                $new_goods_list[] = $v;
            }
            foreach ($add_goods_list as $k => $v) {
                $new_goods_list[] = $v;
            }
        }
        foreach ($new_goods_list as $k => $v) {
            $new_goods_list[$k]['cover'] = _uri('file', $v['cover'], 'path');
        }

        return $new_goods_list;
    }

    /**
     * 获取商品拥有的sku属性值列表
     */
    public function get_sku_list($params = array()) {
        if (isset($params['goods_id']) && $params['goods_id']) {
            $filter['goods_id'] = $params['goods_id'];
            $sku_filter['goods_id'] = $params['goods_id'];
        }
        //         $sku_filter['`stock` >'] = 0;
        $filter['sku_id'] = _model('sku')->getFields('id', $sku_filter);

        $goods_status = _uri('goods', $params['goods_id'], 'status');
        if (!$goods_status) {
            return array();
        }

        $list = array();
        $property_ids = _model('sku_relation')->getFields('property_id', $filter);
        $property_value_ids = _model('sku_relation')->getFields('property_value_id', $filter);
        if (!empty($property_ids)) {
            $list = _model('property')->getList(array('id' => array_unique($property_ids)));
            //$list = _model('property')->getList(array('id'=>$property_ids));
            foreach ($list as $k => $v) {
                $property_value_list = _model('property_value')->getList(array(
                    'property_id' => $v['id'],
                    'id' => $property_value_ids
                ));
                foreach ($property_value_list as $key => $value) {
                    $file_id = _uri('icon_relation', array(
                        'goods_id' => $filter['goods_id'],
                        'property_value_id' => $value['id']
                    ), 'attachment_id');
                    $property_value_list[$key]['icon'] = _uri('attachment', $file_id, 'path');
                    $property_value_list[$key]['tmp_value'] = _uri('sku_relation', array(
                        'property_id' => $v['id'],
                        'property_value_id' => $value['id'],
                        'goods_id' => $filter['goods_id']
                    ), 'tmp_value');
                }
                $list[$k]['property_value'] = $property_value_list;
            }
        }

        return $list;
    }

    /**
     * 获取商品图片列表
     */
    public function get_image_list($params = array()) {
        if (isset($params['goods_id']) && $params['goods_id']) {
            $filter['res_id'] = $params['goods_id'];
        }
        $filter['res_name'] = 'goods';

        if (!$filter['res_id']) {
            return array();
        }

        $cover = _uri('goods', $filter['res_id'], 'cover');
        $attachment_ids = _model('attachment_relation')->getFields('attachment_id', $filter);

        $list = array();
        if (!empty($attachment_ids)) {
            $attachment_list = _model('attachment')->getList($attachment_ids);
            foreach ($attachment_list as $k => $v) {
                if ($v['id'] == $cover) {
                    array_unshift($list, $v['path']);
                    continue;
                }
                $list[] = $v['path'];
            }
        }

        return $list;
    }

    // 获取最近购买的商品列表
    public function get_buy_list($params = array()) {
        $filter['user_id'] = user_helper::get_user_id();
        if (isset($params['user_id']) && $params['user_id']) {
            $filter['user_id'] = $params['user_id'];
        }
        if (isset($params['per_page']) && $params['per_page']) {
            $this->per_page = $params['per_page'];
        }

        $goods_ids = array();
        $goods_list = array();
        $order_list = _model('order')->getList(array('user_id' => $filter['user_id']), ' ORDER BY `add_time` DESC ');
        if (!empty($order_list)) {
            foreach ($order_list as $k => $v) {
                $order_relation_list = _model('order_relation')->getList(array('order_id' => $v['id']));
                foreach ($order_relation_list as $key => $value) {
                    $goods_id = _uri('goods_snapshot', $value['goods_snapshot_id'], 'goods_id');
                    if (count($goods_ids) == $this->per_page) {
                        break;
                    }
                    if ($goods_id && (count($goods_ids) == 0 || !in_array($goods_id, $goods_ids))) {
                        $goods_ids[] = $goods_id;
                    } else {
                        continue;
                    }
                    $goods_info = _uri('goods', $goods_id);
                    $goods_info['price'] = $value['goods_price'];
                    $goods_info['cover'] = goods_helper::get_goods_cover($value['sku_id'], $goods_id);
                    $goods_list[] = $goods_info;
                }
            }
        }

        return $goods_list;
    }

    public function get_not_pay_num() {
        $filter = array(
            'status' => 1,
            'progress' => 1
        );
        $order_list = _model('order')->getList($filter);
        $not_pay_goods_list = array();

        foreach ($order_list as $k => $v) {
            $order_relation_list = _model('order_relation')->getList(array('order_id' => $v['id']));
            foreach ($order_relation_list as $key => $value) {
                if (isset($not_pay_goods_list[$value['goods_id']])) {
                    $not_pay_goods_list[$value['goods_id']] += $value['goods_num'];
                } else {
                    $not_pay_goods_list[$value['goods_id']] = $value['goods_num'];
                }
            }
        }
        return $not_pay_goods_list;
    }
}

?>